package event.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JComponent;

import main.model.Settings;
import event.model.NarwhalEvent;

@SuppressWarnings("serial")
public class EventInfoScreen extends JComponent implements MouseListener {

	private final int SIZE_MINI = 20;
	private final int SIZE_MAXI = 680;
	private boolean expanded;
	private NarwhalEvent model;

	private final String ARRANGER_TOPIC = "Arrangeras av";
	private final String CLOSES_TOPIC = "Anmälan öppen tills";
	private final String CLOSES_IS_CLOSED = "Anmälan är stängd";
	private final String CLOSES_IS_FULL = "Antal platser är slut";

	private String text = "";
	private String header = "Rubrik";
	private String arranger = "";
	private String when = "Den nåntingde någon gång";
	private String closes = "Stänger i morgon";
	
	private EventFrame frame;

	public EventInfoScreen(NarwhalEvent model, EventFrame frame) {
		this.model = model;
		this.frame = frame;

		addMouseListener(this);
	}

	@Override
	public void update(Graphics g) {
		paint(g);
	}

	@Override
	public void paint(Graphics g) {

		paintHeader(g);
		paintFooter(g);
		paintBody(g);
		
	}

	private void paintHeader(Graphics g) {
		g.setFont(new Font("Arial", Font.BOLD, 18));
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, 0, getBounds().width, 20);
		g.setColor(Color.BLACK);
		g.drawString(header, 5, 16);
	}

	private void paintFooter(Graphics g) {
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(0, getBounds().height - 20, getBounds().width, 20);
		g.setColor(Color.BLACK);

		String text;
		if (!expanded && isEnabled()) {
			text = "Datatekniksektionens eventanmälningssystem";

		} else {
			text = "Tryck här för att anmäla dig";
		}

		FontMetrics fm = g.getFontMetrics();
		int x = getBounds().width / 2 - fm.stringWidth(text) / 2;
		int y = getBounds().height + 20 - fm.getHeight();
		g.drawString(text, x, y);
	}

	private void paintBody(Graphics g) {
		int height = getBounds().height - 40;
		int width = getBounds().width - 20;
		if (height <= 0)
			return;

		Image img = createImage(width, height);
		Graphics2D g2 = (Graphics2D) img.getGraphics();
		FontMetrics fm = g2.getFontMetrics();
		g2.setColor(Color.WHITE);
		g2.fillRect(0, 0, width, height);
		g2.setColor(Color.LIGHT_GRAY);
		g2.drawLine(0, 0, 0, height);
		g2.drawLine(width - 1, 0, width - 1, height);
		g2.drawLine(0, (int) (fm.getHeight() * 2.5), width, (int) (fm
				.getHeight() * 2.5));
		g2.setColor(Color.BLACK);
		g2.setFont(new Font("Arial", Font.BOLD, 13));
		g2.drawString(ARRANGER_TOPIC, 10, fm.getHeight());
		g2.drawString(arranger, 10, 2 * fm.getHeight());
		if (model.isActiveClosed()) {
			g2.setColor(Color.RED);
			g2.drawString(CLOSES_IS_CLOSED, width
					- fm.stringWidth(CLOSES_IS_CLOSED) - 10, (int) (1.5 * fm
					.getHeight()));
			g2.setColor(Color.BLACK);
		} else if (!model.placesLeft()) {
			g2.setColor(Color.RED);
			g2.drawString(CLOSES_IS_FULL, width
					- fm.stringWidth(CLOSES_IS_FULL) - 10, (int) (1.5 * fm
					.getHeight()));
			g2.setColor(Color.BLACK);
		} else {
			g2.drawString(CLOSES_TOPIC, width - fm.stringWidth(CLOSES_TOPIC)
					- 10, fm.getHeight());
			g2.drawString(closes, width - fm.stringWidth(closes) - 10, (int) 2
					* fm.getHeight());
		}

		g2.drawString(when, width / 2 - (fm.stringWidth(when) / 2),
				(int) (1.5 * fm.getHeight()));
		g2.setFont(new Font("Arial", Font.PLAIN, 13));
		String[] rows = text.split("\n");
		for (int i = 0; i < rows.length; i++)
			g2.drawString(rows[i], 15, (int) (1.5 * fm.getHeight()) * i
					+ fm.getHeight() * 4);
		g.drawImage(img, 10, 20, null);
	}

	public void expand() {
		if (isEnabled()) {
			setEnabled(false);
			header = model.getActiveTitle();
			text = model.getActiveBody();
			arranger = model.getActiveArranger();
			when = model.getActiveOccures();
			closes = model.getActiveCloses();
			expanded = true;
			new Expander(SIZE_MAXI);
		}
	}

	public void collapse() {
		if (isEnabled()) {
			setEnabled(false);
			new Expander(SIZE_MINI);
			expanded = false;
		}
	}

	private class Expander extends Thread {
		private int goal;

		public Expander(int goal) {
			this.goal = goal;
			start();
		}

		@Override
		public void run() {
			setEnabled(false);
			while (getBounds().height != goal) {
				Rectangle r = getBounds();
				if (r.height < goal) {
					r.height += 10;
				} else {
					r.height -= 10;
				}
				setBounds(r);
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			setEnabled(true);
			frame.trigger();
		}
	}

	private void click() {
		if (getParent().isEnabled() && expanded && isEnabled()) {
			frame.moveMouse();
			new EventPopUp(model, frame);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		if (new Settings().clickToClick())
			click();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		if (new Settings().enterToClick())
			click();
	}

	@Override
	public void mouseExited(MouseEvent arg0) {

	}

	@Override
	public void mousePressed(MouseEvent arg0) {

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}
}
